/*


 */

package jas.rpc.server;

import jas.util.HashBijectiveMap;
import jas.util.BijectiveMap;
import java.io.Serializable;

/**
 *
 *
 */
public class ObjectIdBankImpl implements ObjectIdBank, Serializable {
    
    private BijectiveMap<String, Object> objects = new HashBijectiveMap<String, Object>();
    private int nextAutoId = 1;
    private static final String NULL_ID = "null";

    
    public Object getObjectWithId(String id) {
        return objects.getSecondary(id);
    }
    
    public String getId(Object o) {
        if (o == null)
            return NULL_ID;
        String id = objects.getPrimary(o);
        if (id == null) {
            id = nextId();
            objects.add(id, o);
        }
        return id;
    }
     
    private String nextId() {
        return "id:" + (nextAutoId++);
    }

    public void bind(String id, Object o) {
        objects.add(id, o);
    }

}
